package com.javasm.mingming.news.controller;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.javasm.mingming.common.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.javasm.mingming.common.utils.TokenUtil;
import com.javasm.mingming.news.entity.News;
import com.javasm.mingming.news.service.NewsService;
import com.javasm.mingming.news.vo.DynamicVo;
import com.javasm.mingming.news.vo.NewsData;
import com.javasm.mingming.news.vo.NewsModel;
import com.javasm.mingming.news.vo.NewsResult;
import org.springframework.http.*;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * (News)表控制层
 *
 * @author makejava
 * @since 2025-01-16 09:47:16
 */
@RestController
@RequestMapping("/news")
public class NewsController {
    /**
     * 服务对象
     */
    @Resource
    private NewsService newsService;

    @GetMapping("/test1")
    public R test1(){
        //远程接口调用的对象,Spring封装好的
        RestTemplate restTemplate = new RestTemplate();
        //先访问自己的一个接口测试
        String url = "http://127.0.0.1:8080/dynamics/page?pageNum=1&pageSize=5";
        //返回的数据类型是ResponseEntity
        ResponseEntity<R> responseEntity = restTemplate.getForEntity(url, R.class);
        //获取body
        R body = responseEntity.getBody();
        //获取header
        HttpHeaders headers = responseEntity.getHeaders();

        return body;
    }
    @Resource
    RestTemplate restTemplate;
    @GetMapping("/test2/{id}")
    public R test2(@PathVariable Integer id){
        String url = "http://127.0.0.1:8080/dynamics/{0}";
        R r = restTemplate.getForObject(url, R.class, id);
        return r;
    }
    //post请求
    @GetMapping("/test3")
    public R test3(){
        String url = "http://127.0.0.1:8080/dynamics/save";
        //参数
        MultiValueMap<String,Object> map = new LinkedMultiValueMap<>();
        //相当于一个表单提交
        map.add("content","新闻动态1");
        map.add("likenum",10);
        map.add("uid",20);
        map.add("type",1);
        //设置header
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        //设置 request对象
        HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<>(map, headers);
        ResponseEntity<R> rResponseEntity = restTemplate.postForEntity(url, request, R.class);

        return rResponseEntity.getBody();
    }

    @GetMapping("/test4")
    public R test4(){
        String url = "http://127.0.0.1:8080/dynamics/update";
        MultiValueMap<String,Object> map = new LinkedMultiValueMap<>();
        map.add("id",113);
        map.add("content","美好时光海苔");
        restTemplate.put(url,map);
        return R.ok();
    }

    @GetMapping("/test5")
    public R test5(){
        String url = "http://127.0.0.1:8080/dynamics/del/{0}";
        restTemplate.delete(url,101);
        return R.ok();
    }
    @GetMapping("/test6")
    public R test6(){
        String url = "http://127.0.0.1:8080/dynamics/save";
        MultiValueMap<String,Object> map = new LinkedMultiValueMap<>();
        //相当于一个表单提交
        map.add("content","新闻动态1");
        map.add("likenum",10);
        map.add("uid",20);
        map.add("type",1);
        //header对象
        HttpHeaders headers = new HttpHeaders();
        headers.add(TokenUtil.Server_Token_Key,TokenUtil.generateToken("20"));
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        //request
        HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<>(map, headers);

        ResponseEntity<R> response = restTemplate.exchange(url, HttpMethod.POST, request, R.class);
        return response.getBody();
    }

    @GetMapping("/test7")
    public R test7(){
        String url = "http://127.0.0.1:8080/dynamics";

        DynamicVo vo = new DynamicVo();
        vo.setUid(20);
        vo.setContent("测试json动态");
        vo.setType(1);
        vo.setLikenum(999);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<DynamicVo> http = new HttpEntity<>(vo, headers);
        return restTemplate.postForObject(url, http, R.class);
    }


    @GetMapping("/list")
    public R newsList(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                      @RequestParam(value = "pageSize",defaultValue = "3") Integer pageSize){
        //查询 新闻的信息
        //新闻接口，有访问次数限制，所以，不能直接从第三方查询，要从本地查询
        List<NewsModel> list = newsService.queryList(pageNum,pageSize);
        return R.ok(list);
    }

    @GetMapping("/sync")
    public R syncNews(){
        //查询 远程接口 第三方数据，把查询结果 存入本地Redis
        newsService.syncNews();
        return R.ok();
    }

    /**
     * 分页查询所有数据
     *
     * @param page 分页对象
     * @param news 查询实体
     * @return 所有数据
     */
    @GetMapping
    public R selectAll(Page<News> page, News news) {
        return R.ok(this.newsService.page(page, new QueryWrapper<>(news)));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public R selectOne(@PathVariable Serializable id) {
        return R.ok(this.newsService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param news 实体对象
     * @return 新增结果
     */
    @PostMapping
    public R insert(@RequestBody News news) {
        return R.ok(this.newsService.save(news));
    }

    /**
     * 修改数据
     *
     * @param news 实体对象
     * @return 修改结果
     */
    @PutMapping
    public R update(@RequestBody News news) {
        return R.ok(this.newsService.updateById(news));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    public R delete(@RequestParam("idList") List<Long> idList) {
        return R.ok(this.newsService.removeByIds(idList));
    }
}

